﻿using BenchmarkDotNet.Attributes;
using Orleans.TestingHost;
using System;
using System.Threading.Tasks;

namespace Puppy.CommonService
{
    public class IDGenerateActorBenchmark
    {
        private TestCluster _testCluster;
        private IIDGenerateActor _idActor;

        [Benchmark]
        [Arguments(100)]
        [Arguments(1000)]
        [Arguments(10000)]
        public async Task GenerateId(int count)
        {
            for (int i = 0; i < count; i++)
            {
                await _idActor.NewID();
            }
        }

        [Benchmark]
        [Arguments(100)]
        [Arguments(1000)]
        [Arguments(10000)]
        public async Task GenerateIds(int count)
        {
            await _idActor.NewIDBatch(count);
        }

        [GlobalSetup]
        public void DeploySilo()
        {
            var builder = new TestClusterBuilder();
            builder.Options.ServiceId = Guid.NewGuid().ToString();
            _testCluster = builder.Build();
            _testCluster.Deploy();

            _idActor = _testCluster.GrainFactory.GetGrain<IIDGenerateActor>(Guid.Empty);
        }

        [GlobalCleanup]
        public void Clean()
        {
            _testCluster.StopAllSilos();
        }
    }
}
